<template>
  <div class="content">
    <div class="row">
      <div class="col-md-10">
        <el-form ref="form" :model="form" label-width="80px" style="padding: 10px;">
          <div class="col-md-4">
            <el-form-item label="权限名称">
              <el-input v-model="form.name" placeholder="请输入权限名称"></el-input>
            </el-form-item>
          </div>
          <div class="col-md-2">
            <el-button type="primary" size="medium" @click="addoperation(1)">添加</el-button>
          </div>
        </el-form>
      </div>
      <mu-divider/>
      <mu-dialog title="权限修改" width="360" :open.sync="openSimple">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="权限名称" prop="operation_name">
            <el-input type="text" v-model="ruleForm.operation_name" auto-complete="off"
                      placeholder="请权限目录名称"></el-input>
          </el-form-item>
        </el-form>
        <el-button slot="actions" flat color="primary" @click="closeSimpleDialog" size="small">关闭</el-button>
        <el-button slot="actions" flat type="primary" @click="addoperation(2)" size="small">提交</el-button>
      </mu-dialog>
      <!--<div class="col-md-3" style="background-color: #E4E7ED;min-height: 520px;padding-right: 1px;">-->

      <!--</div>-->
      <div class="col-md-12" style="background-color:#F2F6FC;min-height: 520px;">
        <el-table
          :data="tableData"
          border
          style="width: 100%">
          <el-table-column
            prop="operation_name"
            label="姓名"
            width="150"
            >
          </el-table-column>
          <el-table-column
            prop="create_time"
            label="创建时间"
            width="150">
          </el-table-column>
          <el-table-column
            label="操作"
            width="150">
            <template slot-scope="scope">
              <el-button @click="handleClick(scope.row)" type="text" size="small">删除</el-button>
              <!--<el-button type="text" size="small" @click="handleClick2(scope.row)">编辑</el-button>-->
              <el-button type="text" size="small" @click="handleClick3(scope.row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>
<script>
  import {addoperationInfo, getoperationlist, DeleteoperationInfo, updateoperationInfo} from '../api/api';

  export default {
    data() {
      return {
        form: {
          name: ''
        },
        ruleForm: {
          operation_name: '管理员',
        },
        rules: {
          operation_name: [
            {required: true, message: '请输入角色名称', trigger: 'blur'},
            //{ validator: validaePass }
          ]
        },
        updatename: false,
        parent_id: '',
        openSimple: false,
        defaultProps: {
          children: 'children',
          label: 'label'
        },
        tableData: [],
        loading: false,
        operation_id:'',
        openSimple: false,
      }
    },
    methods: {
      openSimpleDialog() {
        this.openSimple = true;
      },
      closeSimpleDialog() {
        this.openSimple = false;
        this.parent_id = '';
      },
      addoperation(n) {
        var _this = this;
        var name = '';
        if (n == 1) {
          name = this.form.name;
        } else {
          name = this.ruleForm.operation_name;
        }
        if (name != '' && this.updatename == false) {
          var addParams = {operation_name: name};
          addoperationInfo(addParams)
            .then(responsedata => {
              //NProgress.done();
              let {status, data} = responsedata;
              if (status !== 200) {
                this.$message({
                  message: '请求失败',
                  type: 'error'
                });
              } else {
                var descritpion = data.descritpion;
                if (data.status == '90000') {
                  this.$message({
                    message: '提交成功',
                    type: 'success'
                  });
//                  var roleList = data.values;
                  this.initDatesousre();
                } else {
                  this.$message({
                    message: descritpion,
                    type: 'error'
                  });
                }

              }
            })
            .catch(function (response) {
              _this.$notify.error({
                title: '错误',
                message: '网络请求失败'
              });
              console.log(response);
            });

        } else if (name !== '' && this.updatename == true) {
          var updateParams = {operation_name: name, operation_id: this.operation_id};
          updateoperationInfo(updateParams)
            .then(responsedata => {
              //NProgress.done();
              let {status, data} = responsedata;
              if (status !== 200) {
                this.$message({
                  message: '请求失败',
                  type: 'error'
                });
              } else {
                var descritpion = data.descritpion;
                if (data.status == '90000') {
                  this.$message({
                    message: '提交成功',
                    type: 'success'
                  });
                  var VideoList = data.values;
                  this.initDatesousre();
                } else {
                  this.$message({
                    message: descritpion,
                    type: 'error'
                  });
                }

              }
            })
            .catch(function (response) {
              _this.$notify.error({
                title: '错误',
                message: '网络请求失败'
              });
              console.log(response);
            });
          this.updatename = false;
          this.parent_id = '';
        }
        this.closeSimpleDialog();
        console.log('submit!');
      },
      handleClick(row) {
        var _this = this;
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            var deleteParams = {operation_id: row.operation_id};
            DeleteoperationInfo(deleteParams)
              .then(responsedata => {
                let {status, data} = responsedata;
                if (status !== 200) {
                  this.$message({
                    message: '请求失败',
                    type: 'error'
                  });
                } else {
                  var descritpion = data.descritpion;
                  if (data.status == '90000') {
                    this.$message({
                      type: 'success',
                      message: '删除成功!'
                    });
                    this.initDatesousre();
                  } else {
                    this.$message({
                      type: 'error',
                      message: descritpion
                    });
                  }
                }
              })
              .catch(function (response) {
                _this.$notify.error({
                  title: '错误',
                  message: '网络请求失败'
                });
                console.log(response);
              });


          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });
          });
        console.log(row);
      },
      handleClick3(row) {
        var operation_id = row.operation_id;
        this.operation_id = operation_id;
        this.updatename=true;
        this.openSimple = true;
        this.ruleForm.operation_name=row.operation_name;
      },
      initDatesousre() {
        var _this = this;
        var loginParams = {};
        var jsonstr = JSON.stringify(loginParams);
        getoperationlist(jsonstr)
          .then(responsedata => {
            _this.logining = false;
            //NProgress.done();
            let {status, data} = responsedata;
            if (status !== 200) {
              this.$message({
                message: '请求失败',
                type: 'error'
              });
            } else {
              var List = data.values;
              _this.tableData = List;
            }
          })
          .catch(function (response) {
            _this.logining = false;
            _this.$notify.error({
              title: '错误',
              message: '网络请求失败'
            });
            console.log(response);
          });
      },
    },
    mounted() {
      this.initDatesousre();
    }
  }
</script>
